/**
  ******************************************************************************
  * @file    User/tk_user.c
  * @author  HUGE-IC Application Team
  * @version V1.0.0
  * @date    05-20-2022
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 TAIXIN-IC</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "systick.h"
#include "main_func.h"
#include "uart.h"
#include <stdio.h>
#include <math.h>
#include "config.h"


//数据位数
#define FLAME_TAB_LEN   (3)
#define TEMP_TAB_LEN    (11)
#define WARM_TAB_LEN    (4)	
#define BACK_TAB_LEN    (6)	

//计时
#define TK_IDLE_CNT		(4000)
u32  idle_cnt = 0;//休眠计时

u8 smg_play = 0;//数码管位选	
// 共阴数码管编码：  0(00), 1(01), 2(02), 3(03), 4(04), 5(05), 6(06), 7(07), 8(08), 9(09),  
code u16 num_tab[] ={0x003f,0x0006,0x00db,0x008f,0x00e6,0x00ed,0x00fd,0x0007,0x00ff,0x00ef,
							//10~22//   A,     B,     C,      D,     E,     F,     G,     H,     I,     J,     K,     L,     M,
										 0x00f7,0x128f,0x0039, 0x120f,0x0079,0x0071,0x00B9,0x00f6,0x1209,0x0000,0x0000,0x0038,0x0536,
							//23~35//   N,     O,     P,     Q,     R,     S,     T,     U,     V,     W,     X,     Y,     Z,
										 0x0936,0x003f,0x0000,0x0000,0x08F3,0x018d,0x1201,0x003E,0x0000,0x2836,0x2D00,0x10e2,0x0000,
							//36~49//   a,     b,     c,      d,     e,     f,     g,     h,     i,     j,     k,     l,     m,
										 0x0000,0x0000,0x00d8, 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
					    //49~61//   n,     o,     p,     q,     r,     s,     t,     u,     v,     w,     x,     y,     z,
										 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
							//61~62//   -,      ,
										 0x00c0,0x0000,0xFFFF
										};   
#define DP  0x4000				 
										
	
										
void Digit_display(char *s);

//数码管预显示存放数组
volatile unsigned int show[4] = {0};
bit  L1=0,L2=0,L3=0,L4=0,L5=0,L6=0,L7=0;//单个LED显示
bit  flashing_flag;
bit  dp_flag;
//长按返回标志位
extern volatile bit __tk_long_key_flag;

//功能 
bit  on_off = 0;// 开关标志位
bit  temp_led_flag = 0;//温度显示标志位
bit  warm_led_flag = 0;//加热显示标志位
bit  flame_led_flag = 0;//火焰显示标志位
bit  temp_c_f = 0;//温度单位标志位
u32  time_delay = 0;//计时
u8  temp_data = 0;//温度挡位
u8  warm_data = 0;//加热挡位
u8  flame_data = 0;//火焰挡位
bit lock_flag = 1;//锁定标志位
bit led_bright = 1; //灯亮度高低


/** @addtogroup Template_Project
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Touchkey  debug uart init function
  * @param  None
  * @retval None
  */
#if   TK_DEBUG_EN
void debug_gpio_config(void)
{
    P2_MD1   &= ~GPIO_P24_MODE_SEL(0x03); 
    P2_MD1   |=  GPIO_P24_MODE_SEL(0x01); 
    FOUT_S24  =  GPIO_FOUT_UART1_TX;
}
#endif
//串口发送
#define uart1_putc(c)   ll_uart_putchar(c)
void ll_uart_putchar(char uart_data)
{
    //while(!(UART1_STA & UART_TX_DONE(0x01)));
    UART1_DATA = uart_data;
    //while(!(UART1_STA & UART_TX_DONE(0x01)));
}

//数码管段选
void SMG_SHOW(unsigned int m)
{
    if(m & 0x0001){SMG_A = 1;}     else{SMG_A = 0;}
		if(m & 0x0002){SMG_B = 1;}     else{SMG_B = 0;}
    if(m & 0x0004){SMG_C = 1;}     else{SMG_C = 0;}
    if(m & 0x0008){SMG_D = 1;}     else{SMG_D = 0;}
    if(m & 0x0010){SMG_E = 1;}     else{SMG_E = 0;}
    if(m & 0x0020){SMG_F = 1;}     else{SMG_F = 0;} 
    if(m & 0x0040){SMG_G1 = 1;}    else{SMG_G1 = 0;}
		if(m & 0x0080){SMG_G2 = 1;}    else{SMG_G2 = 0;}
		if(m & 0x0100){SMG_H = 1;}     else{SMG_H = 0;}
    if(m & 0x0200){SMG_J = 1;}     else{SMG_J = 0;}
    if(m & 0x0400){SMG_K = 1;}     else{SMG_K = 0;}
    if(m & 0x0800){SMG_L = 1;}     else{SMG_L = 0;}
    if(m & 0x1000){SMG_M = 1;}     else{SMG_M = 0;}
    if(m & 0x2000){SMG_N = 1;}     else{SMG_N = 0;}
		if(m & 0x4000){SMG_DP = 1;}    else{SMG_DP = 0;}
}

//单个LED显示
void led_single(void)
{
	if(L1)LED1=1;else LED1=0;
	if(L2)LED2=1;else LED2=0;
	if(L3)LED3=1;else LED3=0;
	if(L4)LED4=1;else LED4=0;
	if(L5)LED5=1;else LED5=0;
	if(L6)LED6=1;else LED6=0;
	if(L7)LED7=1;else LED7=0;
	SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 0; 
}
//数码管初始化
void led_init(void)
{
	
	WDT_KEY  =  0X55;
	IO_MAP  &=~ 0X01;
	WDT_KEY  =  0XBB;
	P0_MD0  &= ~(GPIO_P00_MODE_SEL(0x3)|GPIO_P01_MODE_SEL(0x3)|GPIO_P02_MODE_SEL(0x3)|GPIO_P03_MODE_SEL(0x3));
	P0_MD0  |=  (GPIO_P00_MODE_SEL(0x1)|GPIO_P01_MODE_SEL(0x1)|GPIO_P02_MODE_SEL(0x1)|GPIO_P03_MODE_SEL(0x1));
	FOUT_S00 = GPIO_FOUT_AF_FUNC;//F
	FOUT_S01 = GPIO_FOUT_AF_FUNC;//G1
	FOUT_S02 = GPIO_FOUT_AF_FUNC;//G2
	FOUT_S03 = GPIO_FOUT_AF_FUNC;//J
	
	P0_MD1  &= ~(GPIO_P04_MODE_SEL(0x3)|GPIO_P05_MODE_SEL(0x3)|GPIO_P06_MODE_SEL(0x3)|GPIO_P07_MODE_SEL(0x3));
	P0_MD1  |=  (GPIO_P04_MODE_SEL(0x1)|GPIO_P05_MODE_SEL(0x1)|GPIO_P06_MODE_SEL(0x1)|GPIO_P07_MODE_SEL(0x1));
	FOUT_S04 = GPIO_FOUT_AF_FUNC;//H
	FOUT_S05 = GPIO_FOUT_AF_FUNC;//COM3
	FOUT_S06 = GPIO_FOUT_AF_FUNC;//A
	FOUT_S07 = GPIO_FOUT_AF_FUNC;//K	
	
	P1_MD0  &= ~(GPIO_P11_MODE_SEL(0x3)|GPIO_P12_MODE_SEL(0x3)|GPIO_P13_MODE_SEL(0x3));
	P1_MD0  |=  (GPIO_P11_MODE_SEL(0x1)|GPIO_P12_MODE_SEL(0x1)|GPIO_P13_MODE_SEL(0x1));
	FOUT_S11 = GPIO_FOUT_AF_FUNC;//C
	FOUT_S12 = GPIO_FOUT_AF_FUNC;//COM4
	FOUT_S13 = GPIO_FOUT_AF_FUNC;//B
	
	P1_MD1  &= ~(GPIO_P14_MODE_SEL(0x3)|GPIO_P17_MODE_SEL(0x3));
	P1_MD1  |=  (GPIO_P14_MODE_SEL(0x1)|GPIO_P17_MODE_SEL(0x1));
	FOUT_S14 = GPIO_FOUT_AF_FUNC;//D
	FOUT_S17 = GPIO_FOUT_AF_FUNC;//L
	
	P2_MD0  &= ~(GPIO_P20_MODE_SEL(0x3)|GPIO_P21_MODE_SEL(0x3)|GPIO_P22_MODE_SEL(0x3)|GPIO_P23_MODE_SEL(0x3));
	P2_MD0  |=  (GPIO_P20_MODE_SEL(0x1)|GPIO_P21_MODE_SEL(0x1)|GPIO_P22_MODE_SEL(0x1)|GPIO_P23_MODE_SEL(0x1));
	FOUT_S20 = GPIO_FOUT_AF_FUNC;//E
	FOUT_S21 = GPIO_FOUT_AF_FUNC;//DP
	FOUT_S22 = GPIO_FOUT_AF_FUNC;//M
	FOUT_S23 = GPIO_FOUT_AF_FUNC;//N
	
	P2_MD1  &= ~(GPIO_P24_MODE_SEL(0x3));
	P2_MD1  |=  (GPIO_P24_MODE_SEL(0x1));
	FOUT_S24 = GPIO_FOUT_AF_FUNC;//COM1
	
	P3_MD0  &= ~(GPIO_P30_MODE_SEL(0x3)|GPIO_P31_MODE_SEL(0x3));
	P3_MD0  |=  (GPIO_P30_MODE_SEL(0x1)|GPIO_P31_MODE_SEL(0x1));
	FOUT_S30 = GPIO_FOUT_AF_FUNC;//COM5
	FOUT_S31 = GPIO_FOUT_AF_FUNC;//COM2
	
	    // 配置IO的驱动能力
    P0_DRV0  = GPIO_P00_DRV_SEL(0x1F);
    P0_DRV1  = GPIO_P01_DRV_SEL(0x1F);
    P0_DRV2  = GPIO_P02_DRV_SEL(0x1F);
    P0_DRV3  = GPIO_P03_DRV_SEL(0x1F);
    P0_DRV4  = GPIO_P04_DRV_SEL(0x1F);
    P0_DRV5  = GPIO_P05_DRV_SEL(0x1F);
    P0_DRV6  = GPIO_P06_DRV_SEL(0x1F);
    P0_DRV7  = GPIO_P07_DRV_SEL(0x1F);

    P1_DRV1  = GPIO_P11_DRV_SEL(0x1F);
    P1_DRV2  = GPIO_P12_DRV_SEL(0x1F);
    P1_DRV3  = GPIO_P13_DRV_SEL(0x1F);
    P1_DRV4  = GPIO_P14_DRV_SEL(0x1F);
    P1_DRV7  = GPIO_P17_DRV_SEL(0x1F);
    P2_DRV0  = GPIO_P20_DRV_SEL(0x1F);
    P2_DRV1  = GPIO_P21_DRV_SEL(0x1F);
    P2_DRV2  = GPIO_P22_DRV_SEL(0x1F);
    P2_DRV3  = GPIO_P23_DRV_SEL(0x1F);
    P2_DRV4  = GPIO_P24_DRV_SEL(0x1F);        
    P3_DRV0  = GPIO_P30_DRV_SEL(0x1F);
    P3_DRV1  = GPIO_P31_DRV_SEL(0x1F);
	
	//初始化全灭
	SMG_A=SMG_B=SMG_C=SMG_D=SMG_E=SMG_F=SMG_G1=SMG_G2=SMG_H=SMG_J=SMG_K=SMG_L=SMG_M=SMG_N=SMG_DP=0;
	SMG_COM1=SMG_COM2=SMG_COM3=SMG_COM4=SMG_COM5=1;
}
//串口1初始化
void uart_init(void)
{
#define UART1_BAUD               (9600UL)
#define UART1_UART_BAUD          ((SYSCLK-UART1_BAUD)/(UART1_BAUD))
#if 1
    // P16-->TX    P15-->RX
    P1_MD1   &= ~(GPIO_P15_MODE_SEL(0x3)|GPIO_P16_MODE_SEL(0x3));
    P1_MD1   |=  (GPIO_P16_MODE_SEL(0x1));

    
    FOUT_S16 |=  GPIO_FOUT_UART1_TX;            // 配置P16为UART1_TX
    FIN_S8   |=  GPIO_FIN_SEL_P15;              // 配置P15为UART1_RX
#else
		// P17-->TX    P20-->RX
		P1_MD1   &= ~GPIO_P17_MODE_SEL(0x3);
    P1_MD1   |=  (GPIO_P17_MODE_SEL(0x1));
	
		P2_MD0   &= ~GPIO_P20_MODE_SEL(0x3);
		P2_MD0   |=  GPIO_P20_MODE_SEL(0x1);

    
    FOUT_S17 |=  GPIO_FOUT_UART1_TX;            // 配置P17为UART1_TX
    FIN_S8   |=  GPIO_FIN_SEL_P20;              // 配置P20为UART1_RX
		
#endif
    __EnableIRQ(UART1_IRQn);                    // 打开UART模块中断
                                      // 打开总中断
    
    UART1_BAUD1 = (UART1_UART_BAUD >> 8) & 0xFF; // 配置波特率高八位
    UART1_BAUD0 =  UART1_UART_BAUD & 0xFF;       // 配置波特率低八位
    UART1_CON0  =  UART_STOP_BIT(0x0)  |         // 8bit数据，1bit停止位，使能中断
                   UART_RX_IRQ_EN(0x1) |
                   UART_EN(0x1);
}
//串口1中断函数
void UART1_IRQHandler(void) interrupt UART1_IRQn
{
    // 进入中断设置IP，不可删除
    __IRQnIPnPush(UART1_IRQn);
    
    // ---------------- 用户函数处理 -------------------
    
    // 接收中断
    if(UART1_STA & UART_RX_DONE(0x1)){
			uint8_t c;
			c = UART1_DATA;
			uart_ringbuffer_rx_write(PORT1, c);
			set_uart1_recv_start_flag(TRUE);
			set_uart1_recv_counter(0);
		}
		// 发送中断
		
    // 退出中断设置IP，不可删除
    __IRQnIPnPop(UART1_IRQn);
}


//定时器0初始化，使能总中断
void timer0_init(void)
{
    __EnableIRQ(TMR0_IRQn);                                                         // 使能timer0中断
    IE_EA = 1;                                                                      // 使能总中断
    
#define PEROID_VAL               (SYSCLK/128/1000 - 1)                               // 周期值=系统时钟/分频/频率 - 1
    // 设置timer0的计数功能，配置一个频率为1kHz的中断
    TMR_ALLCON = TMR0_CNT_CLR(0x1);                                                 // 清除计数值
    TMR0_PRH   = TMR_PERIOD_VAL_H((PEROID_VAL >> 8) & 0xFF);                        // 周期值
    TMR0_PRL   = TMR_PERIOD_VAL_L((PEROID_VAL >> 0) & 0xFF);
    TMR0_CONH  = TMR_PRD_PND(0x1)    | TMR_PRD_IRQ_EN(0x1);                         // 计数等于周期时允许发生中断
    TMR0_CONL  = TMR_SOURCE_SEL(0x7) | TMR_PRESCALE_SEL(0x7) | TMR_MODE_SEL(0x1);   // 选择系统时钟，128分频，计数模式

}

//定时器2中断函数
void timer2_init(void){
		__EnableIRQ(TMR2_IRQn);                                                         // 使能timer0中断
    IE_EA = 1; 
		#define PEROID_VAL1               (SYSCLK/1/8000 - 1)                               // 周期值=系统时钟/分频/频率 - 1
    // 设置timer2的计数功能，配置一个频率为1kHz的中断
    TMR_ALLCON = TMR2_CNT_CLR(0x1);                                                 // 清除计数值
    TMR2_PRH   = TMR_PERIOD_VAL_H((PEROID_VAL1 >> 8) & 0xFF);                        // 周期值
    TMR2_PRL   = TMR_PERIOD_VAL_L((PEROID_VAL1 >> 0) & 0xFF);
    TMR2_CONH  = TMR_PRD_PND(0x1)    | TMR_PRD_IRQ_EN(0x1);                         // 计数等于周期时允许发生中断
    TMR2_CONL  = TMR_SOURCE_SEL(0x7) | TMR_PRESCALE_SEL(0x0) | TMR_MODE_SEL(0x1);   // 选择系统时钟，128分频，计数模式
		IP2 = ((0x01) << 0);  //设置优先级
}
static uint16_t i;
void TIMR2_IRQHandler(void) interrupt TMR2_IRQn
{
		__IRQnIPnPush(TMR2_IRQn);
		if(TMR2_CONH & TMR_PRD_IRQ_EN(0x1)) {
      TMR2_CONH |= TMR_PRD_IRQ_EN(0x1);
			// 周期中断
			i++;
			//if(i > 5000) i = 0;
			smg_play++;
			if(smg_play == 11) smg_play = 0;
			//数码管位选
			/* 闪烁500ms一次 */
			if(!flashing_flag || ((i % 5000) < 2500)){
				if(i % 5 < 2){
					if(smg_play % 2 == 0){
						SMG_SHOW(0x0000);
						SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
					}
					else if(smg_play==1){
						SMG_COM1 = 0;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
						SMG_SHOW(show[0]);
					}
					else if(smg_play==3){
						SMG_COM1 = 1;SMG_COM2 = 0;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
						if(!dp_flag) SMG_SHOW(show[1]);
						else         SMG_SHOW(show[1] | DP);
					}
					else if(smg_play==5){
						SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 0;SMG_COM4 = 1;SMG_COM5 = 1;
						SMG_SHOW(show[2]);
					}
					else if(smg_play==7){
						SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 0;SMG_COM5 = 1;
						SMG_SHOW(show[3]);
					}
				}
				else{
					SMG_SHOW(0x0000);
					SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
				}
			}
			else{
				SMG_SHOW(0x0000);
				SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
			}
			if(smg_play == 8){
					SMG_SHOW(0x0000);
					SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
			}
			else if(smg_play == 9){
					if(led_bright == 1){
						if(i % 10 < 4){
							led_single();
							SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 0;
						}
						else{
							LED1=0;LED2=0;LED3=0;LED4=0;LED5=0;LED6=0;LED7=0;
							SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 0;
						}
					}
					else{
						if(i % 10 < 1){
							led_single();
							SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 0;
						}
						else{
							LED1=0;LED2=0;LED3=0;LED4=0;LED5=0;LED6=0;LED7=0;
							SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 0;
						} 
					}
			}
			else if(smg_play == 10){
				SMG_SHOW(0x0000);
				SMG_COM1 = 1;SMG_COM2 = 1;SMG_COM3 = 1;SMG_COM4 = 1;SMG_COM5 = 1;
			}
		}
		__IRQnIPnPop(TMR2_IRQn);
}
//定时器0中断函数
void TIMR0_IRQHandler(void) interrupt TMR0_IRQn
{
    // 进入中断设置IP，不可删除
    __IRQnIPnPush(TMR0_IRQn);
    
    // ---------------- 用户函数处理 -------------------
    
    // 周期中断
    if(TMR0_CONH & TMR_PRD_PND(0x1)) {
				TMR0_CONH |= TMR_PRD_PND(0x1);          // 清除pending 
				idle_cnt++;
				time_delay++;
				systick();
				uart1_recv_time_check();
			  
		}
    // 退出中断设置IP，不可删除
    __IRQnIPnPop(TMR0_IRQn);
}

/**
  * @brief  用户代码初始化函数接口.
  * @param  None
  * @retval None
  */
void user_init(void)
{
	led_init();
	timer0_init();
	timer2_init();
	uart_init();
	flashing_flag = 0;
}

void Digit_all_set(uint8_t stutus){
	if(stutus == ON){
		show[0] = 0xFFFF;
		show[1] = 0xFFFF;
		show[2] = 0xFFFF;
		show[3] = 0xFFFF;
	}
	else{
		show[0] = 0x0000;
		show[1] = 0x0000;
		show[2] = 0x0000;
		show[3] = 0x0000;
	}
}

void Digit_display(char *s){
	u8 i;
	for(i = 0; i < 4; i++){
		if(*(s+i) >= '0' && *(s+i) <= '9') 
			*(s+i) = *(s+i) - '0' + 0;
		else if(*(s+i) >= 'A' && *(s+i) <= 'Z') 
			*(s+i) = *(s+i) - 'A' + 10;
		else if(*(s+i) >= 'a' && *(s+i) <= 'z') 
			*(s+i) = *(s+i) - 'a' + 36;
		else if(*(s+i) == '-') 
			*(s+i) = 62;
		else if(*(s+i) == ' ') 
			*(s+i) = 61;
		else
			*(s+i) = 61;
	}
	
	//uart1_putc(*(s+0));
	show[0] = num_tab[*(s+0)];
	show[1] = num_tab[*(s+1)];
	show[2] = num_tab[*(s+2)];
	show[3] = num_tab[*(s+3)];
}

void Special_display(enum show_type id, int8_t num){
	char s[4];
	char c = 'C';
	int16_t temp;
	temp = num;
	if(id == TYPE_TEMP_C || id == TYPE_TEMP_F || id == TYPE_SETTEMP){
		if(id == TYPE_TEMP_C) c = 'C';
		else if(id == TYPE_TEMP_F){
			temp = 320 + (temp * 18);
			if(temp % 10 >= 5)  temp = temp / 10 + 1;
			else temp = temp / 10;
			c = 'F';
		}
		if(temp >= 0 && temp < 10)            sprintf(s, "  %d%c", (uint16_t)temp, c);
		else if(temp >= 10 && temp < 100)     sprintf(s, " %d%c", (uint16_t)temp, c);
		else if(temp >= 100 && temp < 1000)   sprintf(s, "%d%c", (uint16_t)temp, c);
		else if(temp <= 0 && temp > -10)      {temp = (int8_t)abs((int16_t)temp); sprintf(s, " -%d%c", (uint16_t)temp, c);}
		else if(temp <= -10 && temp > -100)   {temp = (int8_t)abs((int16_t)temp); sprintf(s, "-%d%c", (uint16_t)temp, c);}
		
		if(id == TYPE_SETTEMP){
			L1 = L2 = L3 = L4 = L7 = 0;
			L5 = L6 = 1;
		}
	}
	else if(id == TYPE_FLAME){
		sprintf(s, " F-%d", (uint16_t)num);
	}
	else if(id == TYPE_HEAT){
		if(num == 0) sprintf(s, " H-0");
		else if(num == 1) sprintf(s, " FAN");
		else if(num == 2) sprintf(s, " H-1");
		else sprintf(s, " H-U");
	}
	else if(id == TYPE_ERR){
		if(num >= 0 && num < 10)            sprintf(s, " E-%d", (uint16_t)num);
		else if(num >= 10 && num < 100)     sprintf(s, "E-%d", (uint16_t)num);
	}
	else if(id == TYPE_POWER){
		if(num == ON)           sprintf(s, "  ON");
		else if(num == OFF)     sprintf(s, " OFF");
	}
	else if(id == TYPE_RTCWEEK || id == TYPE_ONWEEK || id == TYPE_OFFWEEK){
		if(num == 1)           sprintf(s, " MON");
		else if(num == 2)      sprintf(s, " TUE");
		else if(num == 3)      sprintf(s, " WED");
		else if(num == 4)      sprintf(s, "THER");
		else if(num == 5)      sprintf(s, " FRI");
		else if(num == 6)      sprintf(s, " SAT");
		else if(num == 7)      sprintf(s, " SUN");
		
		if(id == TYPE_RTCWEEK) L1 = L2 = L3 = L4 = L5 = L6 = L7 = 0;
		else if(id == TYPE_ONWEEK) {L1 = L2 = L3 = L4 = L6 = L7 = 0; L5 = 1;}
		else if(id == TYPE_OFFWEEK){L1 = L2 = L3 = L4 = L5 = L7 = 0; L6 = 1;}
	}
	else if(id == TYPE_ONTIME || id == TYPE_OFFTIME){
		uint8_t hour, min;
		hour = num / 2;
		min = (num % 2) * 30;
		if(num == 48)
			sprintf(s, "----");
		else
			sprintf(s, "%02d%02d", (uint16_t)hour, (uint16_t)min);
		
		if(id == TYPE_ONTIME) {L1 = L2 = L3 = L4 = L6 = L7 = 0; L5 = 1;}
		else if(id == TYPE_OFFTIME){L1 = L2 = L3 = L4 = L5 = L7 = 0; L6 = 1;}
	}
	else if(id == TYPE_RTCHOUR){
		uint8_t min;
		Get_Config(&min, Config, CF_RTC_MIN);
		sprintf(s, "%02d%02d", (uint16_t)num, (uint16_t)min);
	}
	else if(id == TYPE_RTCMIN){
		uint8_t hour;
		Get_Config(&hour, Config, CF_RTC_HOUR);
		sprintf(s, "%02d%02d", (uint16_t)hour, (uint16_t)num);
	}
	else if(id == TYPE_RTC){
		uint8_t hour, min;
		Get_Config(&hour, Config, CF_RTC_HOUR);
		Get_Config(&min, Config,  CF_RTC_MIN);
		sprintf(s, "%02d%02d", (uint16_t)hour, (uint16_t)min);
	}
	Digit_display(s);
}

void Led_display(uint8_t id, bit status){
	uint8_t power;
	if(id == 1) L1 = status;
	else if(id == 2) L2 = status;
	else if(id == 3) L3 = status;
	else if(id == 4) L4 = status;
	else if(id == 5) L5 = status;
	else if(id == 6) L6 = status;
	else if(id == 7) L7 = status;
	Get_Config(&power, Config, CF_POWER);
	if(power == ON) led_bright = 1;
	else            led_bright = 0;
}

void set_digital_flashing(bit sw){
	flashing_flag = sw;
}








/**
  * @brief  用户代码循环执行函数接口.
  * @param  None
  * @retval None
*/


void user_handle(void){
	main_func();
			
}
/*************************** (C) COPYRIGHT 2022 TAIXIN-IC ***** END OF FILE *****/
